//
//  BusinessFramework.mm
//  LBBase
//
//  Created by Gary on 15/7/1.
//  Copyright (c) 2015年 Lebo. All rights reserved.
//

#import "BusinessFramework.h"
#import "BusinessModule.h"
#import "BusinessListener.h"
#import "CommonDefine.h"
#import "ModuleDefine.h"

#import <UIKit/UIKit.h>
#import <map>
#import <vector>

@interface BusinessFramework () {
    
}

@end

@implementation BusinessFramework

static BusinessFramework *sharedInstance;
static std::map<unsigned int, BusinessModule<BusinessModuleProtocol>*> moduleMap;  //存储着所有的业务模块
static std::vector<id<BusinessListenerProtocol>> listenerPool; //存储着所有的监听者
static std::vector<std::vector<id<BusinessListenerProtocol>>*> listenerPoolList;  //通知给监听者所用

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        sharedInstance = [[[self class] alloc] init];
    });
    return sharedInstance;
}

- (void)registerBusinessModule:(BusinessModule<BusinessModuleProtocol>*)module {
    if (module) {
        moduleMap.insert(std::pair<unsigned int, BusinessModule*>(module.moduleID, module));
    }
}

- (BOOL)findBusinessListener:(id<BusinessListenerProtocol>)listener {
    std::vector<id<BusinessListenerProtocol>>::iterator iter;
    for (iter = listenerPool.begin(); iter != listenerPool.end(); iter++) {
        if (*iter == listener) {
            return YES;
        }
    }
    return NO;
}

- (void)registerBusinessListener:(id<BusinessListenerProtocol>)listener {
    if (listener) {
        listenerPool.push_back(listener);
        unsigned long size = listenerPoolList.size();
        for (int i = 0; i < size; i++) {
            listenerPoolList[i]->push_back(listener);
        }
    }
}

- (void)unregisterBusinessListener:(id<BusinessListenerProtocol>)listener {
    LogDebugFunc;
    if (listener) {
        std::vector<id<BusinessListenerProtocol>>::iterator iter;
        for (iter = listenerPool.begin(); iter != listenerPool.end(); iter++) {
            if (*iter == listener) {
                listenerPool.erase(iter);
                break;
            }
        }
        
        unsigned long size = listenerPoolList.size();
        for (int i = 0; i < size; i++) {
            std::vector<id<BusinessListenerProtocol>> *v = listenerPoolList[i];
            std::vector<id<BusinessListenerProtocol>>::iterator iter;
            for (iter = v->begin(); iter != v->end(); iter++) {
                if (*iter == listener) {
                    v->erase(iter);
                    break;
                }
            }
        }
    }
}

- (void)broadcastBusinessNotification:(unsigned int)notifyID inParam:(id)inParam {
    std::vector<id<BusinessListenerProtocol>> *v = new std::vector<id<BusinessListenerProtocol>>(listenerPool);
    listenerPoolList.push_back(v);
    
    while (v->begin() != v->end()) {
        id<BusinessListenerProtocol> listener = *(v->begin());
        v->erase(v->begin());
        [listener onBusinessNotify:notifyID inParam:inParam];
    }
    listenerPoolList.pop_back();
    delete v;
    v = NULL;
}

- (int)callBusinessFunc:(unsigned int)businessID inParam:(id)inParam {
    id p = nil;
    return [self callBusinessFunc:businessID inParam:inParam outParam:&p];
}

- (int)callBusinessFunc:(unsigned int)businessID inParam:(id)inParam outParam:(id*)outParam {
    unsigned int moduleID = ModuleID(businessID);
    std::map<unsigned int, BusinessModule<BusinessModuleProtocol>*>::iterator iter = moduleMap.find(moduleID);
    if (iter != moduleMap.end()) {
        BusinessModule<BusinessModuleProtocol> *module = iter->second;
        return [module callBusinessFunc:FuncID(businessID) inParam:inParam outParam:outParam];
    }
    
    LogError(@"BusinessModule not found, id: %d", moduleID);
    return -1;
}

@end
